<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        歌曲添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/index.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/index.js"></script>

    <!--<style>
        .imgs{display: none;}
        .picture{display: none;}
    </style>-->

    <!--百度编辑器-->
    <script src="./ueditor/ueditor.config.js"></script>
    <script src="./ueditor/ueditor.all.min.js"></script>
    <script src="./ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>

<div class="layui-card">
    <form class="layui-form layui-form-pane" id="addSong" action="../music" method="POST" enctype="multipart/form-data" >

                <input type="hidden" name="op" value="add">
				<div class="layui-form-item">
					<label for="musicName" class="layui-form-label"> 
						<span class="x-red"></span>歌曲名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="musicName" name="musicName" required=""
							lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="singgerName" class="layui-form-label"> 
						<span class="x-red"></span>歌手名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="singgerName" name="singgerName" required=""
							lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="albumName" class="layui-form-label"> 
						<span class="x-red"></span>专辑名
					</label>
					<div class="layui-input-inline">
						<input type="text" id="albumName" name="albumName" required=""
							lay-verify="required" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="musicStyle" class="layui-form-label"> 
						<span class="x-red"></span>音乐风格
					</label>
					<div class="layui-input-inline">
						    <select name="musicStyle" id="musicStyle">
                                <option value="">---请选择音乐风格---</option>
                            </select>
					</div>
                </div>
				<div class="layui-form-item">
					<label for="theme" class="layui-form-label"> 
						<span class="x-red"></span>音乐主题
					</label>
					<div class="layui-input-inline">
						    <select name="theme" id="theme">
                                <option value="">---请选择音乐主题---</option>
                            </select>
					</div>
                </div>
                <div class="layui-form-item">
					<label for="mood" class="layui-form-label"> 
						<span class="x-red"></span>音乐心情
					</label>
					<div class="layui-input-inline">
						    <select name="mood" id="mood">
                                <option value="">---请选择音乐心情---</option>
                            </select>
					</div>
                </div>
                <div class="layui-form-item">
					<label for="scene" class="layui-form-label"> 
						<span class="x-red"></span>音乐场景
					</label>
					<div class="layui-input-inline">
						    <select name="scene" id="scene">
                                <option value="">---请选择音乐场景---</option>
                            </select>
					</div>
                </div>
                <div class="layui-form-item">
					<label for="languages" class="layui-form-label"> 
						<span class="x-red"></span>音乐语种
					</label>
					<div class="layui-input-inline">
						    <select name="languages" id="languages">
                                <option value="">---请选择音乐语种---</option>
                            </select>
					</div>
                </div>
                <div class="layui-form-item">
                	<label for="releaseTime" class="layui-form-label"> 
						<span class="x-red"></span>发布日期
					</label>
					<div class="layui-inline layui-show-xs-block">
						<input class="layui-input" autocomplete="off" placeholder="请选择发布日期"
							name=releaseTime id="releaseTime">
					</div>
				</div>
				<div class="layui-form-item">
					<label for="musicLyric" class="layui-form-label"> 
						<span class="x-red"></span>歌曲歌词
					</label>
					<div class="layui-input-inline">
						<input type="file" name="musicLyric"  accept="application/x-digitalloca" id="musicLyric" class="layui-btn">
					</div>
                </div>
                <div class="layui-form-item">
					<label for="musicMp3Path" class="layui-form-label"> 
						<span class="x-red"></span>歌曲音频
					</label>
					<div class="layui-input-inline">
						<input type="file" name="musicMp3Path"  accept="audio/mpeg,audio/midi,audio/ogg,audio/mp4,audio/wav,audio/x-ms-wma,audio/mp4a-latm" id="musicMp3Path" class="layui-btn">
					</div>
                </div>
                <div class="layui-form-item">
					<label for="musicImgPath" class="layui-form-label"> 
						<span class="x-red"></span>歌曲海报
					</label>
					<div class="layui-input-inline">
						<input type="file" name="musicImgPath"  accept="image/jpg,image/jpeg,image/png,image/gif" id="musicImgPath" class="layui-btn">
					</div>
                </div>
                <div class="layui-form-item">
					<label for="musicImgPath" class="layui-form-label"> 
						<span class="x-red"></span>海报预览
					</label>
                        <img src="" id="preview-images" name="preview-images" style="width: 400px;height: 200px;"/>
                        <input id="avatar"   name="" required="" type="hidden"  value="images">
                </div>
                <div class="layui-form-item">
                        <!--  修改成提交按钮 -->
                        <button class="layui-btn" type="submit" lay-filter="add" lay-submit=""> 添加 </button>
                 </div>
    </form>
</div>
<!-- 引入jQuery.js -->
<script src="./js/jquery.js"></script>
<!-- 引入jQuery-form.js 异步提交表单插件 -->
<script src="./js/jquery-form.js"></script>
<script>
	$(function(){
		// 获取歌曲风格
		$.getJSON("../MusicStyleServlet",function(result){
			let content = "";
			$.each(result,function(index,musicStyle){
				content += "<option value=\""+ musicStyle.styleId +"\">"+ musicStyle.styleName +"</option>";
			});
			$("#musicStyle").append(content);
			
		});
		//  获取主题
		$.getJSON("../music",{op:"theme"},function(result){
			let content = "";
			$.each(result,function(index,theme){
				content += "<option value=\""+ theme.themeId +"\">"+ theme.themeName +"</option>";
			});
			$("#theme").append(content);
		});
		//  获取心情
		$.getJSON("../music",{op:"mood"},function(result){
			let content = "";
			$.each(result,function(index,mood){
				content += "<option value=\""+ mood.moodId +"\">"+ mood.moodName +"</option>";
			});
			$("#mood").append(content);
		});
		//  获取场景
		$.getJSON("../music",{op:"scene"},function(result){
			let content = "";
			$.each(result,function(index,scene){
				content += "<option value=\""+ scene.sceneId +"\">"+ scene.sceneName +"</option>";
			});
			$("#scene").append(content);
		});
		//  获取语种
		$.getJSON("../music",{op:"languages"},function(result){
			let content = "";
			$.each(result,function(index,languages){
				content += "<option value=\""+ languages.languagesId +"\">"+ languages.languagesName +"</option>";
			});
			$("#languages").append(content);
		});
		
		
		// 2.异步提交表单
		$("#addSong").submit(function(){
			// 异步提交表单
			$(this).ajaxSubmit(function(result){
				 // 提示
				 layui.use('layer', function(){
					 layer.msg(result,{icon:1,time:2000});
					
				 });
				 // 调用父窗口中的查询方法
				 window.parent.Song_search();
				 // 关闭当前子窗口
				 var index = parent.layer.getFrameIndex(window.name);
                 parent.layer.close(index);
			});
			// 限制默认的表单提交
			return false;
		});
		
		// 3.生成图片预览
		$("#musicImgPath").change(function(){
			// 加入前端对上传文件内容类型和大小限制
			// 获取上传的文件
			let imgFile = this.files[0];
			// 判断是否有选择文件
			if(imgFile != null){
				// 正则表达式验证选择的文件是否是限制的MIME类型
				let regexImageFilter = /^(?:image\/png|image\/jpeg|image\/jpg)$/; 
				// 匹配
				if(!regexImageFilter.test(imgFile.type)){
					 layui.use('layer', function(){
							layer.msg("请选择有效的图片:image/jpg,image/jpeg,image/png,image/gif",{icon:2,time:2000});
					 });
					 $("#musicImgPath").val("");
					
				}else if(imgFile.size > 40000){
					 layui.use('layer', function(){
							layer.msg("图片大小不能超过40K",{icon:2,time:2000});
					 });
					 $("#musicImgPath").val("");
				}else{
					// 生成预览图
					// 构建文件读取流对象
					let fileReader = new FileReader();
					// 利用读取流对象读取文件的内容
					fileReader.readAsDataURL(imgFile);
					// 利用读取流对象加载事件
					fileReader.onload = function(){
						// 将头像显示在指定的框中
						document.getElementById("preview-images").src = this.result;
					}
				}
			}else{
				document.getElementById("preview-images").src = "";
			}
			
		});
		
	})

</script>
<script type="text/javascript">
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
   /*  UE.getEditor('content',{initialFrameWidth:600,initialFrameHeight:400,}); */
</script>
<script>
layui.use([ 'laydate', 'form' ], function() {
	var laydate = layui.laydate;
	var form = layui.form;

	// 监听全选
	form.on('checkbox(checkall)', function(data) {

		if (data.elem.checked) {
			$('tbody input').prop('checked', true);
		} else {
			$('tbody input').prop('checked', false);
		}
		form.render('checkbox');
	});

	//执行一个laydate实例
	laydate.render({
		elem : '#releaseTime' //指定元素
	});

});

    layui.use(['laydate','element','layer','form','upload'], function(){
        var  $ = layui.jquery;//jquery
        var lement = layui.element;//面包导航
        var layer = layui.layer;//弹出层
        var form = layui.form;
        var upload = layui.upload;

        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });

        //图片上传接口
        /*layui.upload({
          url: './upload.json' //上传接口
          ,success: function(res){ //上传成功后的回调
              console.log(res);
            $('#LAY_demo_upload').attr('src',res.url);
          }
        });*/
        //指定允许上传的文件类型
        upload.render({
            elem: '#test3'
            ,url: 'https://httpbin.org/post' //改成您自己的上传接口
            ,accept: 'file' //普通文件
            ,done: function(res){
                layer.msg('上传成功');
                console.log(res);
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            var title=$("input[name='title']").val();
            //var content=$("textarea[name='content']").val();

            var article_category_id=$('#article_category_id option:selected') .val();//所属文章类型
            //alert(title);
            //var form=$("#cate_add").serialize();
            if(title==""){
                layer.msg('文章标题不能为空',{icon:5,time:2000});return false;
            }
            if(article_category_id==""){
                layer.msg('文章类型不能为空',{icon:5,time:2000});return false;
            }
            /*if(content){
                layer.msg("文章内容不能为空！",{icon:5,time:2000});return false;
          }*/
            var data = data.field;
            $.ajax({
                type:"post",
                url:"",
                data:data,
                dataType:"json",
                success:function(data){
                    if(data.status==1){
                        layer.msg(data.info, {icon: 6,time:2000},function () {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                        return false;

                    }else{
                        layer.msg(data.info,{icon:5,time:2000});return false;
                    }
                }

            });

        });

    })
</script>
<!--栏目缩略图上传-->
<script>
    function upload(obj,id){
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        formData.append('id', id);//将id追加再id中
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type:"post",
            processData: false,
            contentType: false,
            url:"",
            data:formData,
            success:function(data){
                if(data.status == 1){
                    console.log(data.image_name);
                    layer.closeAll('loading');
                    //layer.msg(data.info,{icon:1,time:1000});
                    $("#pimages").attr('src',data.image_name);
                    $("#avatar").val(data.image_name);
                    $(".imgs").show();
                    return false;
                }else{
                    layer.msg(data.info,{icon:2,time:1000});
                }
            }
        });
    }
</script>

</body>
</html>